const common = require('../../utils/common')
var base64ToSrc = require("../../utils/base64src");
const { getShopProductPoster, shopProductSearch, queryShopProdWeekPage } = require('../../services/stores')
const defaultPage = {
  curPage: 1,
  pageSize: 10,
}
Page({
  data: {
    query: {
      ...defaultPage,
      keyword: "",
      type: "1"
    },
    tabList: [
      { name: '一周总提成', value: '1' },
      { name: '提成', value: '2' },
    ],
    total: 10,
    dataList: [],
    isSharePageShow: false,
    // 分享需要的图片
    sharePageInfo: {
      mainImage: '',
      codeUrl: '',
      address: "",
      price: '',
      shopName: "",
      title: ""
    },
  },
  onLoad: function () {
    this.getDataList()
  },
  getDataList: function () {
    if (this.data.query.curPage > 1 && this.data.dataList.length >= this.data.total) return
    const params = { ...this.data.query }
    queryShopProdWeekPage(params).then((res) => {
      const { total } = res.page
      this.setData({ total })
      if (params.curPage === 1) {
        this.setData({
          dataList: res.data
        })
      } else {
        this.setData({
          dataList: [...this.data.dataList, ...res.data]
        })
      }
    }).catch(err => {
      common.log(err)
    })
  },
  tabChange: function (e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.tabList[index];
    this.updateQuery({ type: item.value, ...defaultPage })
  },
  updateQuery: function (payload) {
    this.setData({
      query: { ...this.data.query, ...payload }
    }, this.getDataList)
  },
  scrollBottom: function () {
    const curPage = ++this.data.query.curPage
    this.updateQuery({ curPage })
  },
  onSearch: function (e) {
    const value = e.detail.value
    this.updateQuery({ keyword: value, ...defaultPage })
  },
  cancelSearch: function () {
    this.updateQuery({ keyword: '', ...defaultPage })
  },
  updateSharePage(payload) {
    this.setData({
      sharePageInfo: { ...this.data.sharePageInfo, ...payload }
    }, this.openSharePage)
  },
  //关闭海报页面
  closeSharePage: function () {
    this.setData({
      isSharePageShow: false,
    });
  },
  //打开海报页面
  openSharePage: function () {
    this.setData({
      isSharePageShow: true,
    });
  },
  //分享生成海报
  onSharePage: function (e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.dataList[index];
    if (!item.mainImage) {
      common.showToast("商品主图不存在!");
      return;
    }
    this.downloadSharePageImages(item);
  },

  //下载分享所需要的图片
  downloadSharePageImages: function (item) {
    const generateCodeData = {
      productId: item.productId,
    }
    getShopProductPoster(generateCodeData).then((response) => {

      const { mainImage, codeUrl, price } = response.data
      Promise.all([common.wxDownloadFile(mainImage), base64ToSrc.imageUrlWithBase64src(codeUrl)]).then((res) => {
        const [tempMainImage, tempCodeUrl] = res
        const toFixedPrice = parseFloat(price).toFixed(2).toString()
        this.updateSharePage({ ...response.data, mainImage: tempMainImage, codeUrl: tempCodeUrl, price: toFixedPrice })
      }).catch(err => {
        common.log(err)
      })
    }).catch(err => {
      common.log(err)
    })
  },
  goSearch: function () {
    wx.navigateTo({ url: `/pages/search-result/index` })
  }
})
